package com.sgb.kjwl.view.ui.adapter.self;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.view.widget.RoundImageView;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.VerifyDriverEntity;

import java.util.List;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/10/15
 */
public class AddProDriverListAdapter extends BaseQuickAdapter<VerifyDriverEntity, BaseViewHolder> {
    private Context mContext;

    public AddProDriverListAdapter(Context context, @Nullable List<VerifyDriverEntity> data) {
        super(R.layout.add_proprietary_driver_list_item, data);
        this.mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, VerifyDriverEntity driverEntity) {
        helper.setText(R.id.phone_num, !TextUtils.isEmpty(driverEntity.getPhone()) ? driverEntity.getPhone() : "");

        RoundImageView imageView = helper.getView(R.id.driver_img);
        if (!TextUtils.isEmpty(driverEntity.getImgUrl())) {
            Glide.with(mContext)
                    .load(driverEntity.getImgUrl())
                    .placeholder(R.drawable.icon_default_profile_photo_round)
                    .error(R.drawable.icon_default_profile_photo_round)
                    .into(imageView);
        }
        if (driverEntity.getStatus() != null) {
            if (driverEntity.getStatus() == 1) {//允许添加
                helper.setVisible(R.id.add_btn, true);
                helper.setVisible(R.id.added_tv, false);
                helper.setText(R.id.name, handleDriverName(driverEntity.getName()));
            } else if (driverEntity.getStatus() == 2) {//已添加
                helper.setText(R.id.name, !TextUtils.isEmpty(driverEntity.getName()) ? driverEntity.getName() : "");
                helper.setVisible(R.id.add_btn, false);
                helper.setVisible(R.id.added_tv, true);
            }
        }
        helper.addOnClickListener(R.id.add_btn);
    }

    /**
     * 司机姓名显示逻辑
     *
     * @param driverName
     * @return
     */
    private String handleDriverName(String driverName) {
        String name = "";
        if (!TextUtils.isEmpty(driverName)) {
            if (driverName.length() == 1) {
                name = driverName;
            } else if (driverName.length() == 2) {
                name = "*" + driverName.charAt(1);
            } else {
                name = driverName.charAt(0) + "*" + driverName.substring(2);
            }
        }
        return name;
    }
}
